package com.lxt.blog.controller;

import com.lxt.blog.dao.ArticleDao;
import com.lxt.blog.dao.ArticleDaoImpl;
import com.lxt.blog.pojo.Article;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
// 沉舟侧畔千帆过，
// 病树前头万木春.
@WebServlet("/server/user")
public class ArticleServlet extends HttpServlet {

    //规定文章状态为T公开F私密
    //创建文章默认ID为0,由数据库指定具体ID
    protected static int DEFAULT_ID = 0;
    protected ArticleDao articleDao= new ArticleDaoImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String key = request.getParameter("key");
        if("getUserInfo".equals(key)){
            //获取登录用户信息
            getUserInfo(request,response);
        }else if("sendArticle".equals(key)){
            //上传文章
            sendArticle(request,response);
        } else if("getUserArticle".equals(key)){
            //获取文章
            getUserArticle(request,response);
        } else if("deleteArticle".equals(key)){
            //删除文章
            deleteArticle(request,response);
        } else if("getAllArticle".equals(key)){
            getAllArticle(request,response);
        }
    }

    /*
     *获取所有文章
     * 返回HTML代码
     */
    protected void getAllArticle(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userId= request.getParameter("usr");
        List<Article> articles = articleDao.selectUserArticle(userId);
        String data="";
        for(Article article:articles){
            data+="<textarea readonly>"+article.getUserid()+"    "+article.getTime()
                    +"     "+article.getCaption()+"\n\n"+article.getArticle()
                    +"</textarea> ";
        }
        response.getWriter().write(data);
    }

    /*
     *获取用户的所有文章
     * 返回HTML代码
     */
    protected void getUserArticle(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userId= (String)(request.getSession().getAttribute("username"));
        List<Article> articles = articleDao.selectUserArticle(userId);
        String data="";
        for(Article article:articles){
            data+="<textarea readonly>"+article.getUserid()+"    "+article.getTime()
                    +"     "+article.getCaption()+"\n\n"+article.getArticle()
                    +"</textarea>  <button id=\""+article.getId()+"\">删除文章</button>  ";
        }
        response.getWriter().write(data);
    }

    /*
     *插入文章
     * 获取session的username
     * 获取标题
     * 获取文章
     * 获取格式化时间
     * 获取状态
     * 成功返回T失败返回F
     */
    protected void sendArticle(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userId = (String)(request.getSession().getAttribute("username"));
        String caption = request.getParameter("caption");
        String artcontent = request.getParameter("artcontent");
        String time = request.getParameter("time");
        String publicd = request.getParameter("publicd");
        Article article = new Article(DEFAULT_ID,userId,caption,artcontent,time,publicd);
        String status = articleDao.insertArticle(article) ? "T":"F";
        //插入成功返回T，失败F
        response.getWriter().write(status);
    }

    /*
     *删除文章
     * 获得id
     * 返回成功失败
     */
    protected void deleteArticle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.valueOf(request.getParameter("id"));
        System.out.println(id);
        String status = articleDao.deleteArticle(id) ? "T":"F";
        response.getWriter().write(status);

    }
    /*
     * 获取登录用户信息
     * 返回用户名字
     */
    protected void getUserInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        String name = "F";
        try {
                name = session.getAttribute("username").toString();
        } catch (Exception e){
            name = "F";
        } finally {
            response.getWriter().write(name);
        }
    }
}
